package com.aaa.qy127.service.impl;

import com.aaa.qy127.dao.DeptDao;
import com.aaa.qy127.entity.Dept;
import com.aaa.qy127.service.DeptService;
import com.aaa.qy127.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("deptServiceImpl")
public class DeptServiceImpl implements DeptService {

    /*
    * 注入 DeptDao
    * */
    @Resource(name = "deptDaoImpl")
    private DeptDao deptDao;


    /**
     * 查询所有的 dept的信息
     * @return
     */
    @Override
    public List<Map<String, Object>> selDept(){
        List<Map<String, Object>> allDept = deptDao.getAllDept();
        return allDept;
    }

    /**
     * 添加或修改 dept的信息
     * @param dept
     * @return
     */
    public@ResponseBody String addDept(Dept dept){
        int i = 0;
        List<Map<String, Object>> oneDept = deptDao.getOneDept(dept.getDname());
        List<Map<String, Object>> oneDeptId = deptDao.getOneDeptId(dept.getDname(), dept.getDeptid());
        if(dept.getDeptid()==null){//如果没有deptid就表示新增
            if (oneDept.size()==0){
                i = deptDao.insertDept(dept);
            }else {
                return "1003";
            }
        }else{
            if (oneDept.size()==0 || oneDeptId.size()==1){
                i = deptDao.updateDept(dept);
            }else {
                return "1003";
            }
        }
        String s = "";
        if(i>0){
            s = "1001";
        }else{
            s = "1002";
        }
        return s;
    }

    /**
     * 查询信息
     * @param dept
     * @return
     */
    public@ResponseBody Result queryAllBook(Dept dept){
        List<Map<String, Object>> list = deptDao.queryAllBook(dept);
        int count = deptDao.getAllCount(dept);
        Result result = new Result(count,list);
        return result;
    }

    /**
     * 修改上下架的状态
     * @param deptid
     * @param status
     * @return
     */
    public@ResponseBody String updateStatus(Integer deptid, Integer status, Integer count){
        if (count == 0 || status ==0){
            int i = deptDao.updateStatus(deptid, status);
            return "1001";
        }else {
            return "1002";
        }

    }

    @Override
    public int max() {
        int maxDept = deptDao.getMaxDept();
        return maxDept;
    }
}
